forum

Home / DeveloperSection / Forums / Java Multiple Inheritance

Java Multiple Inheritance

Samuel Fernandes181105-Nov-2014

I am working with the JavaFX Shape subclasses, and I have run into what I believe is a rather strange issue. My goal is to extend several of these shape subclasses (i.e. RectangleCircle) in order to add my own attributes to these objects. For example, the extension of the Rectangle subclass would look like this:

publicclassMyRectangleextends javafx.scene.shape.RectangleimplementsSpecialInterface{privateSpecialAttributes specialAttributes;}

Where the SpecialInterface can be used to specify the methods related to the new attributes that will be  added to MyRectangle and MyCircle, in this case:

publicinterfaceSpecialInterface{publicSpecialAttributes getSpecialAttributes();publicvoid setSpecialAttributes();}

However, when I try to create service classes that reference these subclasses of Rectangle and Circle, it seems as though I cannot do so generically. Essentially, the problem arises when I need to utilize attributes and methods from both the Shape subclasses and the SpecialInterface interface:

publicclassManipulationService{publicManipulationService(<Undefined> myExtendedShape){ myExtendedShape.onRotate(newEventHandler<>(){}); myExtendedShape.getSpecialAttributes();}}

The issue here is that I cannot create a superclass for my extended shapes that would replace <Undefined> above. Specifically, if I create a superclass, I cannot extend the specific shapes that I want to extend in my subclasses due to a lack of multiple inheritance. If I replace <Undefined> with Shape, though, I then lose access to the methods in SpecialInterface.

I'm sure that this sort of multiple-inheritance problem has been solved before, but I cannot find the solution. I appreciate any and all suggestions on how to handle this situation.


Updated on 05-Nov-2014

Can you answer this question?


Answer

1 Answers

Liked By